package hash;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * @author 十八岁讨厌编程
 * @date 2025/6/27 21:17
 * @PROJECT_NAME leetcode
 * @description
 */

public class Solution15 {

    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<>();
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            Integer flag = null;
            for (int j = i + 1; j < nums.length; j++) {
                if (Integer.valueOf(nums[j]).equals(flag)) continue;
                flag = null;
                int need = -nums[i] - nums[j];
                if (set.contains(need)) {
                    result.add(Arrays.asList(nums[i], nums[j], need));
                    flag = nums[j];
                } else {
                    set.add(nums[j]);
                }
            }
            set.clear();
        }
        return result;
    }

}
